package com.leyou.page.service;

import com.leyou.item.pojo.*;
import com.leyou.page.client.GoodsClient;
import com.leyou.page.client.SpecClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageService {


    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecClient specClient;

    @Autowired
    private FileService fileService;

    /**
     * loadData就是查询方法，返回的map结构大概为Map<"spu",spu>
     *
     * @param spuId
     * @return
     */
    public Map<String, Object> loadData(Long spuId) {
        Map<String, Object> result = new HashMap<>();
        //根据spuId查询spu
        Spu spu = goodsClient.querySpuById(spuId);
        result.put("spu", spu);
        List<Sku> skus = goodsClient.querySkuBySpuId(spuId);
        result.put("skus", skus);
        SpuDetail spuDetail = goodsClient.querySpuDetailById(spuId);
        result.put("spuDetail", spuDetail);
        Map<Long, String> specs = new HashMap<>();

        List<SpecParam> specParams = specClient.querySpecParam(null, spu.getCid3(), null, false);
        specParams.forEach(specParam -> specs.put(specParam.getId(), specParam.getName()));
        result.put("specs", specs);

        //根据分类的id查询分类对应的规格组以及组内的参数
        List<SpecGroup> specGroups = specClient.querySpecGroups(spu.getCid3());

        result.put("specGroups",specGroups);

        return result;

    }
}
